package cn.wgd.zmx.utils;

import cn.wgd.zmx.common.exception.EmException;
import cn.wgd.zmx.common.exception.OwnException;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 对于数据库密码字段，我们使用md5加密
 */
public class MD5Utils {

    public static String encode(String password){
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            Base64.Encoder base64Encoder = Base64.getEncoder();
            return base64Encoder.encodeToString(md5.digest(password.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            throw new OwnException(EmException.MD5_EXCEPTION);
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedOperationException();
        }
    }

}
